//
//  UserManager.m
//  ChineseCollegeStudentsNetwork
//
//  Created by Bazinga on 2018/8/9.
//  Copyright © 2018年 刘喆. All rights reserved.
//

#import "UserManager.h"

NSString * const kUserModelKey = @"user_model_key";

@implementation UserManager
@synthesize loginStatus = _loginStatus;

+ (UserManager *)sharedInstance {
    static dispatch_once_t once;
    static UserManager *sharedInstance;
    dispatch_once(&once, ^{
        sharedInstance = [[UserManager alloc] init];
        [sharedInstance loadData];
    });
    
    return sharedInstance;
}

- (BOOL)loginStatus {
    if (self.userModel) {
        return YES;
    }
    return NO;
}

- (void)setUserModel:(UserModel *)userModel {
    _userModel = userModel;
    [self saveData];
}
#pragma mark - save
- (void)saveData {
    NSString *json = [self.userModel toJSONString];
    [[NSUserDefaults standardUserDefaults]setValue:json forKey:kUserModelKey];
    [[NSUserDefaults standardUserDefaults]synchronize];
}
- (void)loadData {
    NSString *json = [[NSUserDefaults standardUserDefaults]valueForKey:kUserModelKey];
    if (json) {
        self.userModel = [[UserModel alloc]initWithString:json error:nil];
    }
}
+ (void)login {
    if ([[BaseViewController topViewController] isKindOfClass:[LoginViewController class]]) {
        return;
    }
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:loginVC];
    [[BaseViewController topViewController] presentViewController:nav animated:YES completion:^{
        
    }];
}
@end
